package com.bw.a1908.uitls.gfit.display;

import android.os.Handler;

import com.blankj.utilcode.util.LogUtils;
import com.bw.a1908.uitls.gfit.GiftBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * 核心业务代码
 */
public class DisplayController implements IGiftController {

    private List<GiftBean> showLists = new ArrayList<>();
    private List<GiftBean> loadingLists = new ArrayList<>();
    private boolean isRun;
    private static Handler handler  = new Handler();

    private int maxCount;

    public DisplayController(int maxCount) {
        this.maxCount = maxCount;
    }

    @Override
    public List<GiftBean> getShowingList() {
        return showLists;
    }

    @Override
    public List<GiftBean> getLoadingList() {
        return loadingLists;
    }

    @Override
    public void addGift(GiftBean giftBean) {
        if (isShow()) {
            giftBean.createTime = System.currentTimeMillis();
            showLists.add(giftBean);
            start();
        } else {
            loadingLists.add(giftBean);
        }
    }

    @Override
    public void start() {
        //启动线程
        if (isRun) {
            return;
        }
        isRun = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                //循环数据
                LogUtils.d("thread running");
                while (loadingLists.size() > 0 || showLists.size() > 0) {
                    //有等待的
                    if (loadingLists.size() >= 0) {
                        //有无展示的位置 有将其添加满
                        //计算有多少空位置
                        int addCount = 0;
                        if (maxCount - showLists.size() > loadingLists.size()) {
                            addCount = loadingLists.size();
                        } else {
                            addCount = maxCount - showLists.size();
                        }
                        for (int i = 0; i < addCount; i++) {
                            GiftBean bean = loadingLists.get(0);
                            bean.createTime = System.currentTimeMillis();
                            showLists.add(bean);
                            loadingLists.remove(0);
                        }
                    }
                    //展示队列处理完成，对所有展示时间数据排序，0位置为时间最早要销毁到对象
                    Collections.sort(showLists);
                    //测试通过
//                    LogUtils.json(showLists);
                    //通过状态 和 时间 来控制数据回调
                    for (GiftBean bean : showLists) {
                        switch (bean.status) {
                            case 0:
                                //开始展示
                                if (onGiftChanger != null) {
                                    //线程调度
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            onGiftChanger.show(bean);
                                        }
                                    });
                                }
                                //改变状态为展示中
                                bean.status = 1;
                                break;
                            case 1:
                                //展示中
                                if (bean.createTime + bean.showTime <= System.currentTimeMillis()) {
                                    //改展示结束
                                    if (onGiftChanger != null) {
                                        //线程调度
                                        handler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                onGiftChanger.finish(bean);
                                            }
                                        });
                                    }
                                    //改变状态为展示中
                                    bean.status = 2;
                                }
                                break;
                        }
                    }
                    //清除对象
                    clearNext();
                    if (showLists.size() > 0) {
                        try {
                            long time = showLists.get(0).createTime + showLists.get(0).showTime - System.currentTimeMillis();
                            if (time > 0){
                                Thread.sleep(time);
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                isRun = false;
            }
        }).start();
    }

    private void clearNext() {
        Iterator<GiftBean> iterator = showLists.iterator();
        while (iterator.hasNext()) {
            GiftBean bean = iterator.next();
            if (bean.status == 2)
                iterator.remove();
        }
    }

    @Override
    public boolean isShow() {
        return showLists.size() < maxCount;
    }

    @Override
    public void destroy() {

    }

    private OnGiftChanger onGiftChanger;

    public void setOnGiftChanger(OnGiftChanger onGiftChanger) {
        this.onGiftChanger = onGiftChanger;
    }

    public interface OnGiftChanger {
        void show(GiftBean bean);

        void finish(GiftBean bean);
    }
}
